<template>
    <div class="contain-box">
        <div class="box-header">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>销售管理</el-breadcrumb-item>
                <el-breadcrumb-item to="/salesSlip" replace>销售单</el-breadcrumb-item>
                <el-breadcrumb-item>新增销售单</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="box-body">
            <div class="body-top">新增销售单</div>
            <div class="body-set">
                <el-input v-model="addObj.customerName" placeholder="客户" style="width: 200px;margin-right: 10px"
                          size="small"></el-input>
                <el-select v-model="addObj.warehousId" placeholder="请选择出库仓库" size="small"
                           style="width: 200px;margin-right: 10px">
                    <el-option v-for="item in warehousList" :key="item.id" :label="item.warehousName"
                               :value="item.id"></el-option>
                </el-select>
                <el-date-picker
                        v-model="addObj.orderCreateTime"
                        value-format="yyyy-MM-dd"
                        size="small"
                        type="date"
                        placeholder="选择日期">
                </el-date-picker>
            </div>
            <div class="body-add">
                <el-button type="primary" style="background-color: #1abc9c" size="small" @click="showGood">关联商品
                </el-button>
            </div>
            <div class="body-table">
                <el-table
                        :data="chooseData"
                        size="small"
                        border
                        style="width: 100%">
                    <el-table-column
                            type="index"
                            label="序号"
                            width="100px"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="createTime"
                            label="订单日期"
                            align="center"
                    >
                    </el-table-column>

                    <el-table-column
                            prop="name"
                            label="商品名称"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="attributeContent"
                            label="规格型号"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="inventory"
                            label="数量"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="goodMeasurementUnit.name"
                            label="单位"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="price"
                            label="销售单价(元)"
                            align="center"
                    >
                    </el-table-column>

                    <el-table-column
                            label="操作"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-button @click="deleteObj(scope.$index)" size="small" type="text">删 除</el-button>
                        </template>
                    </el-table-column>

                </el-table>
            </div>
            <div class="body-message">
                <p style="font-size: 16px">收货信息 : </p>
                <div class="message-box">
                    <div class="message-son">
                        <span>收货人 : </span> <span>{{addObj.consignee?addObj.consignee:'无'}}</span>
                    </div>
                    <div class="message-son">
                        <span>联系电话 : </span> <span>{{addObj.phone?addObj.phone:'无'}}</span>
                    </div>
                    <div class="message-son">
                        <span>收货地址 : </span> <span>{{addObj.receiveGoodAddr?addObj.receiveGoodAddr:'无'}}</span>
                    </div>
                    <div @click="dialogAddress = true">
                        <img src="../assets/images/edit.png" class="message-img">
                    </div>

                </div>
            </div>
            <div class="body-set">
                <el-input @input="changeRate" v-model="addObj.discountRate" placeholder="折扣率%"
                          style="width: 200px;margin-right: 10px" size="small"></el-input>
                <el-input v-model="addObj.discountMoney" disabled placeholder="折扣金额"
                          style="width: 200px;margin-right: 10px" size="small"></el-input>
                <el-select v-model="addObj.settlementId" placeholder="请选择结算方式" size="small"
                           style="width: 200px;margin-right: 10px">
                    <el-option v-for="item in settlementList" :key="item.id" :label="item.settlementName"
                               :value="item.id"></el-option>
                </el-select>
            </div>
            <div class="body-set">
                <el-input v-model="addObj.remarks" placeholder="请输入订单备注" type="textarea" style="width: 50%" :rows="5"
                          resize="none"
                          size="small"></el-input>
            </div>
        </div>
        <div class="box-submit">
            <div class="submit-left">
                <p>总计金额: <span>￥{{addObj.discountMoney+addObj.actualReceiptMoney}}</span></p>
                <p>实收金额: <span>￥{{addObj.actualReceiptMoney}}</span></p>
            </div>
            <div>
                <el-button size="small" @click="goBack">取 消</el-button>
                <el-button @click="submitAdd" type="primary" size="small" style="background-color: #1abc9c">提交审核
                </el-button>
            </div>
        </div>

        <el-dialog
                title="收货地址"
                :visible.sync="dialogAddress"
                width="30%"
        >
            <div class="dialog-box">
                <el-input v-model="addObj.consignee" placeholder="收货人" size="small"
                          style="margin-bottom: 15px"></el-input>
                <el-input v-model="addObj.phone" placeholder="手机号" size="small" style="margin-bottom: 15px"></el-input>
                <v-distpicker :placeholders="placeholders" @province="onChangeProv"
                              @city="onChangeCity"
                              @area="onChangeArea"></v-distpicker>
                <el-input v-model="fullAddr" placeholder="详细地址" size="small" type="textarea"
                          :rows="2" style="margin-top: 15px" resize="none"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
					<el-button @click="dialogAddress = false" size="small">取 消</el-button>
					<el-button type="primary" @click="submitAddr" size="small"
                               style="background-color: #1abc9c">确 定</el-button>
				  </span>
        </el-dialog>
        <el-dialog
                title="关联商品"
                :visible.sync="dialogTable"
                width="50%"
        >
            <div>
                <el-input clearable v-model="pageObj.name" placeholder="请输入商品名称/商品编号" style="width: 50%" size="small">
                    <el-button @click="getAllGood" slot="append" icon="el-icon-search"></el-button>
                </el-input>
                <el-table
                        :data="tableData"
                        size="mini"
                        tooltip-effect="dark"
                        style="width: 100%;margin-top: 20px"
                        @selection-change="handleSelectionChange">
                    <el-table-column
                            align="center"
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="goodType.name"
                            label="商品分类"
                            align="center"
                    >
                    </el-table-column>

                    <el-table-column
                            prop="name"
                            label="商品名称"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="attributeContent"
                            label="规格型号"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="goodMeasurementUnit.name"
                            label="计量单位"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="inventory"
                            label="库存"
                            align="center"
                    >
                    </el-table-column>
                </el-table>
                <div class="dialog-page">
                    <el-pagination
                            background
                            @current-change="handleSelectionChange2"
                            :current-page="pageObj.pageIndex"
                            :page-size="pageObj.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                </div>
            </div>

            <span slot="footer" class="dialog-footer">
					<el-button @click="dialogTable = false" size="small">取 消</el-button>
					<el-button type="primary" @click="submitChoose" size="small"
                               style="background-color: #1abc9c">确 定</el-button>
				  </span>
        </el-dialog>
    </div>
</template>

<script>
    import VDistpicker from 'v-distpicker'

    export default {
        name: "AddSalesOrder",
        components: {VDistpicker},
        data() {
            return {
                pageObj: {
                    pageIndex: 1,
                    pageSize: 5,
                    name: null
                },
                total: 0,
                chooseData: [],
                chooseList: [],
                placeholders: {
                    province: '请选择省份',
                    city: '请选择市区',
                    area: '请选择县城或区',
                },
                dialogAddress: false,
                dialogTable: false,
                tableData: [],
                settlementList: [],
                fullAddr: null,
                addObj: {
                    orderCreateTime: null,
                    customerName: null,
                    warehousId: null,
                    goodId: [],
                    consignee: null,
                    phone: null,
                    receiveGoodAddr: null,
                    discountRate: null,
                    discountMoney: 0,
                    actualReceiptMoney: 0,
                    settlementId: null,
                    orderRemarks: null
                },
                warehousList: [],
                select: {
                    p: null,
                    c: null,
                    a: null
                }
            }
        },
        created() {
            this.getAllSettlement()
            this.getAllWarehous()
        },
        methods: {
            submitAdd() {
                this.axios.post('/salesOrder/save', this.addObj).then(res => {
                    if (res.data.code === 200) {
                        this.goBack()
                        this.$message.success('提交成功')
                    }
                })
            },
            submitAddr() {
                if (!this.addObj.consignee) {
                    return this.$message.warning('请输入收货人姓名')
                }
                if (!this.addObj.phone) {
                    return this.$message.warning('请输入联系方式')
                }
                if (!this.select.p || !this.select.c || !this.select.a) {
                    return this.$message.warning('请选择区域')
                }
                if (!this.fullAddr) {
                    return this.$message.warning('请输入详细地址')
                }
                this.addObj.receiveGoodAddr = this.select.p + "-" + this.select.c + '-' + this.select.a + '-' + this.fullAddr
                this.dialogAddress = false
            },
            changeRate() {
                if (this.addObj.discountRate) {
                    let m = this.addObj.actualReceiptMoney * this.addObj.discountRate
                    if (m > this.addObj.actualReceiptMoney) {
                        this.addObj.discountRate = null
                        this.addObj.discountMoney = 0
                        this.submitChoose()
                        return this.$message.warning('折扣金额不能大于总金额')
                    }
                    this.addObj.discountMoney = this.addObj.actualReceiptMoney * this.addObj.discountRate
                    this.addObj.actualReceiptMoney = this.addObj.actualReceiptMoney - this.addObj.discountMoney
                } else {
                    // this.addObj.discountMoney = 0
                    this.submitChoose()
                }
            },
            deleteObj(index) {
                this.chooseData.splice(index, 1)
                this.addObj.goodId = []
                this.addObj.actualReceiptMoney = 0
                this.addObj.discountMoney = 0
                if (this.chooseData.length > 0) {
                    for (let i = 0; i < this.chooseData.length; i++) {
                        this.addObj.goodId.push(this.chooseData[i].id)
                        this.addObj.actualReceiptMoney = this.addObj.actualReceiptMoney + this.chooseData[i].price
                    }
                    if (this.addObj.discountRate) {
                        this.addObj.discountMoney = this.addObj.actualReceiptMoney * this.addObj.discountRate
                    }
                }
            },
            submitChoose() {
                this.chooseData = this.chooseList
                this.addObj.goodId = []
                this.addObj.discountMoney = 0
                this.addObj.actualReceiptMoney = 0
                if (this.chooseData.length > 0) {
                    for (let i = 0; i < this.chooseData.length; i++) {
                        this.addObj.goodId.push(this.chooseData[i].id)
                        this.addObj.actualReceiptMoney = this.addObj.actualReceiptMoney + this.chooseData[i].price
                    }
                    if (this.addObj.discountRate) {
                        this.addObj.discountMoney = this.addObj.actualReceiptMoney * this.addObj.discountRate
                    }
                }
                this.dialogTable = false
            },
            handleSelectionChange2(currentPage) {
                this.pageObj.pageIndex = currentPage;
                this.getAllGood();
            },
            showGood() {
                this.getAllGood()
            },
            //查询所有商品
            getAllGood() {
                this.axios.post('/good/findPage', this.pageObj).then(res => {
                    if (res.data.code === 200) {
                        this.tableData = res.data.data.data
                        this.total = res.data.data.total
                        this.dialogTable = true
                    }
                })
            },
            //查询全部结算方式
            getAllSettlement() {
                this.axios.get('/settlement/findAll').then(res => {
                    if (res.data.code === 200) {
                        this.settlementList = res.data.data
                    }
                })
            },
            //查询全部仓库
            getAllWarehous() {
                this.axios.get('/warehous/findAll').then(res => {
                    if (res.data.code === 200) {
                        this.warehousList = res.data.data
                    }
                })
            },
            goBack() {
                this.$router.replace('/salesSlip')
            },
            onChangeProv(e) {
                console.log("省", e);
                this.select.c = null
                this.select.a = null
                if (e.value === '请选择省份') {
                    this.select.p = null
                    return false
                }
                this.select.p = e.value
            },
            onChangeCity(e) {
                console.log("市", e);
                this.select.a = null
                if (e.value === '请选择市区') {
                    this.select.c = null
                    return false
                }
                this.select.c = e.value
            },
            onChangeArea(e) {
                console.log("县", e);
                if (e.value === '请选择县城或区') {
                    this.select.a = null
                    return false
                }
                this.select.a = e.value
            },
            handleSelectionChange(val) {
                console.log(val)
                this.chooseList = val
            }
        }
    }
</script>

<style scoped lang="less">
    .contain-box {
        width: 100%;
        height: 100%;

        /deep/ .distpicker-address-wrapper {
            select {
                width: 32% !important;
                padding: 0px 10px !important;
                height: 35px !important;
                font-size: 12px !important;
                line-height: 35px !important;
                border-radius: 5px;
            }
        }

        .dialog-box {
            width: 100%;
        }

        .dialog-page {
            margin-top: 20px;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: flex-end;

            /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active {
                background-color: #1abc9c;
            }
        }

        /deep/ .el-dialog__body {
            padding: 10px 20px;
        }

        .box-header {
            width: 100%;
            height: 50px;
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .box-body {
            width: 100%;
            border-radius: 5px;
            padding: 20px;
            box-sizing: border-box;
            box-shadow: 0 0 5px 0 #e4e4e4;

            .body-top {
                width: 100%;
                height: 50px;
                text-align: center;
                line-height: 50px;
                border-bottom: 1px solid #e4e4e4;
                font-size: 20px;
                color: #666;
            }

            .body-set {
                margin-top: 20px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: flex-start;
            }

            .body-add {
                margin-top: 20px;
                width: 100%;
            }

            .body-table {
                margin-top: 20px;
                width: 100%;
            }

            .body-message {
                margin-top: 20px;
                width: 100%;

                .message-box {
                    margin-top: 20px;
                    display: flex;
                    align-items: center;
                    cursor: pointer;

                    .message-img {
                        margin-left: 20px;
                        width: 20px;
                        height: 20px;
                    }

                    .message-son {
                        margin-right: 15px;
                        font-size: 14px;
                    }
                }
            }
        }

        .box-submit {
            margin-top: 20px;
            width: 100%;
            border-radius: 5px;
            padding: 20px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 0 5px 0 #e4e4e4;

            .submit-left {
                display: flex;
                font-size: 16px;

                p {
                    margin-right: 20px;

                    span {
                        color: red;
                        font-weight: bold;
                    }
                }
            }
        }
    }
</style>
